package com.xjggb.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/*
* API基本操作
* */

public class Demo01 {


    public static void main(String[] args) {

        show1();

    }

    /*
    * 1. 第一个队伍只保留姓名长度为3的成员
      2. 第一个队伍筛选之后只要前3个人
      3. 第二个队伍只要姓张的成员
      4. 第二个队伍筛选之后不要前两个人
      5. 将两个队伍合并为一个队伍
      6. 根据姓名创建Person对象
      7. 打印整个队伍的Person信息
    * */

    public static void show1(){
        List<String> list1 = Arrays.asList("迪丽热巴", "宋远桥", "苏星河", "老子", "庄子", "孙子", "洪七 公");
        List<String> list2 = Arrays.asList("古力娜扎", "张无忌", "张三丰", "赵丽颖", "张二狗", "张天爱", "张三");

        //第一个队伍
        Stream<String> limit1 = list1.stream()
                .filter(s -> s.length() == 3)
                .limit(3);

        //第二个队伍
        Stream<String> limit2 = list2.stream()
                .filter(s -> s.startsWith("张"))
                .skip(2);

        //合并流
        Stream.concat(limit1, limit2).forEach(System.out::println);


    }
    
    public static void concat(){
        Stream<String> stream1 = Stream.of("a","b","c");
        Stream<String> stream2 = Stream.of("x", "y", "z");
        // 通过concat方法将两个流合并为一个新的流
        Stream.concat(stream1,stream2).forEach(System.out::println);
    }


    public static void mapToInt(){
        getInteger().stream()
                .filter(o->o>2)
                .mapToInt(Integer::intValue)
                .filter(o->o>3)
                .forEach(System.out::println);
    }

    public static void reduce(){
        // 1.求出所有年龄的总和
        Integer reduce = getListPerson().stream()
                .map(Person::getAge)
                .reduce(0, Integer::sum);

        System.out.println("sumAge = " + reduce);

        /*
        * 求最大值
        * */
        Integer reduce1 = getListPerson().stream()
                .map(Person::getAge)
                .reduce(0, Integer::max);
        System.out.println("reduce1 = " + reduce1);


    }
    
    public static void minAndmax(){
        Optional<Integer> min = getList1().stream().map(Integer::parseInt)
                .min((o1, o2) -> o1 - o2);
        System.out.println("min.get() = " + min.get());

        Optional<Integer> max = getList1().stream().map(Integer::parseInt)
                .max((o1, o2) -> o1 - o2);
        System.out.println("max.get() = " + max.get());

    }

    public static void find(){
        Optional<String> first =
                getList1().stream().findFirst();
        System.out.println("first.get() = " + first.get());

        Optional<String> any = getList1().stream().findAny();
        System.out.println("any = " + any.get());

    }

    public static void match(){
        boolean b = getList1().stream()
                .map(Integer::parseInt)
                .sorted((o1, o2) -> o1 - o2)
                .distinct()
//                .allMatch(s -> s > 3);
//                .anyMatch(s -> s > 3);
                .noneMatch(s -> s > 3);
        System.out.println("b = " + b);
    }

    public static void distinct(){
     getList1().stream()
             .map(Integer::parseInt)
             .sorted((o1,o2)->o1-o2)
             .distinct()
             .forEach(System.out::println);
    }
    
    public static void sorted(){
    getList1().stream()
            .map(Integer::parseInt)
            .sorted((o1,o2)->o1-o2)
            .forEach(System.out::println);
    }

    public static void map(){
    getList1().stream().map(Integer::parseInt)
            .forEach((o)->{
                System.out.println("o = " + o);
            });
    }

    public static void skip(){
    getList().stream().skip(3).forEach((o)->{
        System.out.println("o = " + o);
    });
    }

    public static void limit(){
    getList().stream().limit(3).forEach((s -> {
        System.out.println("s = " + s);
    }));
    }


    public static void filter(){
    getList().stream().filter((s)->{
        return s.contains("a");
    }).forEach((p)->{
        System.out.println("p = " + p);
    });
    }
    

    public static void count(){
        System.out.println("getList().stream().count() = " + getList().stream().count());

    }

    public static void forEach(){
        getList().stream().forEach(o->{
            System.out.println("o = " + o);
        });
    }

    public static ArrayList<String> getList(){
        ArrayList<String> list = new ArrayList<>();
        list.add("a1");
        list.add("a2");
        list.add("a3");
        list.add("a4");
        list.add("a5");
        list.add("s6");
        list.add("s7");
        return list;
    }

    public static ArrayList<String> getList1(){
        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        list.add("5");
        list.add("3");
        list.add("2");
        list.add("2");
        list.add("2");
        list.add("6");
        list.add("6");
        list.add("6");
        list.add("7");
        list.add("4");
        list.add("4");
        list.add("4");
        list.add("4");
        return list;
    }


    public static ArrayList<Person> getListPerson(){

        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("张三", 18));
        people.add( new Person("李四", 22));
        people.add(new Person("张三", 13));
        people.add(new Person("王五", 15));
        people.add(new Person("张三", 19));
        return people;
    }

    public static ArrayList<Integer> getInteger(){

        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        return list;

    }


}
